package com.loby.data.mapper;

import com.loby.data.code.pojo.TCodeAdministrativeDivision;
import com.loby.data.code.pojo.TCodeDictCategory;
import org.apache.dubbo.config.annotation.Service;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;
@Mapper
public interface AdministrativeDivisionMapper {

    @Select("select * from t_code_administrative_division where level=#{level}")
    List<TCodeAdministrativeDivision> getAdministrativeDivisionsByLevel(@Param("level")int level);

    @Select("select * from t_code_administrative_division where fk_parent_code=#{pkDivisionCode}")
    List<TCodeAdministrativeDivision> getAdministrativeDivisionsByFkParentCode(@Param("pkDivisionCode")String pkDivisionCode);

    @Select("select * from t_code_administrative_division")
    List<TCodeAdministrativeDivision> getAllAdministrativeDivisions();


    @Select("select * from t_code_administrative_division where pk_division_code =#{adCode}")
    TCodeAdministrativeDivision getAdministrativeDivisionsByCode(@Param("adCode") String adCode);

    /**
     * 返回所有本编码的上级行政区
     * 并按照级别升序排序
     * @param adCode 行政区编码
     * @return 所有响应的上级行政区
     */
    @Select("SELECT `pk_division_code`,`division_name`,`full_phonetic`,`fk_parent_code`,`code_path`,`name_path`,`level`," +
            "`alphabet_code` FROM `t_code_administrative_division` where find_in_set(pk_division_code," +
            "(select code_path from t_code_administrative_division where pk_division_code=#{adCode}))order by `level`")
    List<TCodeAdministrativeDivision>  getAdministrativeDivisionsAllParents(@Param("adCode") String adCode);
    /**
     * 返回所有本编码的所有下级行政区
     * @param adCode 行政区编码
     * @return 所有响应的下级行政区
     */

    @Select("SELECT * FROM t_code_administrative_division WHERE FIND_IN_SET(pk_division_code,get_child_list(#{adCode}))")
    List<TCodeAdministrativeDivision> getChildsByAdCode(@Param("adCode") String adCode);

}
